package com.imddysc.springbootbatch001.utils;

import java.util.Random;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class RandomUtils {

	private static Logger log = LoggerFactory.getLogger(RandomUtils.class);
	
	private static final String NumberStr = "0123456789";
	private static final String LowerAlphaStr = "abcdefghijklmnopqrstuvwxyz";
	private static final String UpperAlphaStr = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
	private static final String AlphaStr = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
	private static final String AlphaNumberStr = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
	
	/** 返回指定长度的随机数字字符串 */
	public static String getRandomNumber(int length) {
		return getRandom(length, NumberStr);
	}
	
	/** 返回指定长度的随机小写字母字符串 */
	public static String getRandomLowerAlpha(int length) {
		return getRandom(length, LowerAlphaStr);
	}
	
	/** 返回指定长度的随机大写字母字符串 */
	public static String getRandomUpperAlpha(int length) {
		return getRandom(length, UpperAlphaStr);
	}
	
	/** 返回指定长度的随机字母字符串 */
	public static String getRandomAlpha(int length) {
		return getRandom(length, AlphaStr);
	}
	
	/** 返回指定长度的随机数字字母字符串 */
	public static String getRandomAlphaNumber(int length) {
		return getRandom(length, AlphaNumberStr);
	}
	

	/** 根据提供的字符串，返回指定长度随机字符串 */
	public static String getRandom(int length, String source) {
		StringBuffer sb = new StringBuffer();
		Random random = new Random();
		for (int i = 0; i < length; i++ ) {
			sb.append(source.charAt(random.nextInt(source.length())));
		}
		return sb.toString();
	}
	
	
	public static void main(String[] args) {
		log.info("字母数字: " + RandomUtils.getRandomAlphaNumber(15) );
		log.info("仅仅数字: " + RandomUtils.getRandomNumber(15) );
		log.info("小写字母: " + RandomUtils.getRandomLowerAlpha(15) );
		log.info("大写字母: " + RandomUtils.getRandomUpperAlpha(15) );
		log.info("仅仅字母: " + RandomUtils.getRandomAlpha(15) );
		
		log.info("other: " + RandomUtils.getRandom(15,"在中央网络安全和信息化委员会领导下国家互联网信息办公室会同中华人民共和国国家发展和改革委员会中华人民共和国工业和信息化部中华人民共和国公安部中华人民共和国国家安全部") );
	}

}
